﻿using System;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows;

namespace BMSPlatform.Control
{
    public class UniformGridWithLines : Panel
    {
        public int Columns
        {
            get { return (int)GetValue(ColumnsProperty); }
            set { SetValue(ColumnsProperty, value); }
        }

        public static readonly DependencyProperty ColumnsProperty =
            DependencyProperty.Register("Columns", typeof(int), typeof(UniformGridWithLines), new PropertyMetadata(1));

        public Brush LineColor
        {
            get { return (Brush)GetValue(LineColorProperty); }
            set { SetValue(LineColorProperty, value); }
        }

        public static readonly DependencyProperty LineColorProperty =
            DependencyProperty.Register("LineColor", typeof(Brush), typeof(UniformGridWithLines), new PropertyMetadata(Brushes.Black));

        protected override Size MeasureOverride(Size availableSize)
        {
            if (Children.Count == 0)
                return new Size();

            int rows = (int)Math.Ceiling((double)Children.Count / Columns);
            double childWidth = availableSize.Width / Columns;
            double childHeight = availableSize.Height / rows;

            foreach (UIElement child in Children)
            {
                child.Measure(new Size(childWidth, childHeight));
            }

            return new Size(availableSize.Width, childHeight * rows);
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            if (Children.Count == 0)
                return base.ArrangeOverride(finalSize);

            int rows = (int)Math.Ceiling((double)Children.Count / Columns);
            double childWidth = finalSize.Width / Columns;
            double childHeight = finalSize.Height / rows;

            for (int i = 0; i < Children.Count; i++)
            {
                int column = i % Columns;
                int row = i / Columns;

                UIElement child = Children[i];
                child.Arrange(new Rect(column * childWidth, row * childHeight, childWidth, childHeight));
            }

            return finalSize;
        }

        protected override void OnRender(DrawingContext dc)
        {
            base.OnRender(dc);

            if (Columns <= 1 || Children.Count == 0)
                return;

            int rows = (int)Math.Ceiling((double)Children.Count / Columns);
            double childWidth = RenderSize.Width / Columns;
            double childHeight = RenderSize.Height / rows;

            Pen pen = new Pen(LineColor, 1);

            for (int i = 1; i < Columns; i++)
            {
                double x = i * childWidth;
                dc.DrawLine(pen, new Point(x, 0), new Point(x, RenderSize.Height));
            }

            for (int i = 1; i < rows; i++)
            {
                double y = i * childHeight;
                dc.DrawLine(pen, new Point(0, y), new Point(RenderSize.Width, y));
            }
        }
    }
}
